<?php

namespace app\admin\command\Task;

use app\admin\command\Api\library\Builder;
use app\admin\model\ReportDaily;
use app\admin\model\ReportMonth;
use app\admin\model\ReportYear;
use app\admin\model\Reportschool as ReportschoolModel;
use app\admin\model\School;
use app\admin\model\StundentVipLogs;
use app\admin\model\User;
use app\admin\model\VipConfig;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;

class ReportSchool extends Command
{
    //每日统计 00：00执行
    protected function configure()
    {
        $this->setName('report:school')->setDescription('Here is the remark ');
    }


    protected function execute(Input $input, Output $output)
    {

        //读取所有学校数据
        $list = (new School())->select();
        foreach ($list as $value){
            $this->handle($value);
        }

    }
    protected function handle($value)
    {
        //读取学生里面所有的班级
        $sql = "SELECT 
count(*) as user_count,
count(DISTINCT grade_id) as grade_count,
count(DISTINCT class_id) as class_count,
count(if(vip_type,id,null)) as vip_count
FROM fa_user WHERE type =2 and school_id = ".$value->id;
        $base_01 = Db::query($sql);
        $grade_count = $base_01[0]["grade_count"];
        $class_count = $base_01[0]["class_count"];
        $student_vip_count = $base_01[0]["vip_count"];
        $student_count = $base_01[0]["user_count"];

        //获取会员具体类型数据
        $vipConfig = (new VipConfig())->select();
        $student_vip_1_count = 0;
        $student_vip_2_count = 0;
        $student_vip_3_count = 0;
        $vip_1_amount = 0;
        $vip_2_amount = 0;
        $vip_3_amount = 0;
        foreach ($vipConfig as $item){
            if ($item["vip_year"] == 1) {
                $student_vip_1_count = (new User())->where("type",2)->where("school_id",$value->id)->where("vip_type",$value['id'])->count();
                //会员总金额
                $vip_1_amount = (new  StundentVipLogs())->where("school_id",$value->id)->where("operate_type","add")->where("vip_type",$value['id'])->sum("amount");
            }elseif ($item["vip_year"] == 2){
                $student_vip_2_count = (new User())->where("type",2)->where("school_id",$value->id)->where("vip_type",$value['id'])->count();
                //会员总金额
                $vip_2_amount = (new  StundentVipLogs())->where("school_id",$value->id)->where("operate_type","add")->where("vip_type",$value['id'])->sum("amount");

            }elseif ($item["vip_year"] == 3){
                $student_vip_3_count = (new User())->where("type",2)->where("school_id",$value->id)->where("vip_type",$value['id'])->count();
                //会员总金额
                $vip_3_amount = (new  StundentVipLogs())->where("school_id",$value->id)->where("operate_type","add")->where("vip_type",$value['id'])->sum("amount");
            }
        }

        //
        //获取时间减一天
        $date = date("Y-m-d",strtotime("-1 day"));
        $month = date("Y-m",strtotime("-1 day"));
        $year = date("Y",strtotime("-1 day"));
        //插入数据
        $report_daily = (new ReportschoolModel())->where("date",$date)->where("type","date")->where("school_id",$value->id)->find();
        if ($report_daily){
            (new ReportschoolModel())->where("id",$report_daily->id)->update(
                [
                    "school_id" => $value->id,
                    "school_name" => $value->name,
                    "type" => "date",
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "update_time" => time(),
                ]
            );
        }else{
            (new ReportschoolModel())->insert(
                [
                    "school_id" => $value->id,
                    "school_name" => $value->name,
                    "type" => "date",
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "create_time" => time(),
                    "update_time" => time(),
                ]
            );
        }


        //月份的进行更新
        $report_month = (new ReportschoolModel())->where("month",$month)->where("type","month")->where("school_id",$value->id)->find();
        if ($report_month){
            (new ReportschoolModel())->where("id",$report_month->id)->update(
                [
                    "school_id" => $value->id,
                    "school_name" => $value->name,
                    "type" => "month",
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "update_time" => time(),
                ]
            );
        }else{
            (new ReportschoolModel())->insert(
                [
                    "school_id" => $value->id,
                    "school_name" => $value->name,
                    "type" => "month",
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "create_time" => time(),
                    "update_time" => time(),
                ]
            );
        }

        //年统计进行更新
        $report_year = (new ReportschoolModel())->where("year",$year)->where("type","year")->where("school_id",$value->id)->find();
        if ($report_year){
            (new ReportschoolModel())->where("id",$report_year->id)->update(
                [
                    "school_id" => $value->id,
                    "school_name" => $value->name,
                    "type" => "year",
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "update_time" => time(),
                ]
            );
        }else{
            (new ReportschoolModel())->insert(
                [
                    "school_id" => $value->id,
                    "school_name" => $value->name,
                    "type" => "year",
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "create_time" => time(),
                    "update_time" => time(),
                ]
            );
        }

        echo "success".PHP_EOL;
    }


}
